package com.zx.lean_ui.Activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.zx.lean_ui.Adapter.MyFragmentPagerAdapter;
import com.zx.lean_ui.R;
import com.zx.lean_ui.fragment.BlankFragment4;

import java.util.ArrayList;
import java.util.List;

public class MainActivity5 extends AppCompatActivity implements View.OnClickListener {

    private ViewPager2 main5Vp;
    private List<Fragment>fragments=new ArrayList<>();
    private LinearLayout llChat, llContacts,llFind, llProfile;
    private ImageView ivChat, ivContacts, ivFind, ivProfile;
    //用来设置当前的按钮为false点击其他图标进行fragment切换时变成灰色
    private ImageView ivCurrent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);
        initView();
        initTabView();
        initPager();
    }
    private void initView() {
        main5Vp = findViewById(R.id.main5_vp);
    }
    private void initTabView() {
        llChat = findViewById(R.id.id_tab_weixin);
        llChat.setOnClickListener(this);
        llContacts = findViewById(R.id.id_tab_contact);
        llContacts.setOnClickListener(this);
        llFind = findViewById(R.id.id_tab_find);
        llFind.setOnClickListener(this);
        llProfile = findViewById(R.id.id_tab_profile);
        llProfile.setOnClickListener(this);
        ivChat = findViewById(R.id.tab_iv_weixin);
        ivContacts = findViewById(R.id.tab_iv_contact);
        ivFind = findViewById(R.id.tab_iv_find);
        ivProfile = findViewById(R.id.tab_iv_profile);

        ivChat.setSelected(true);
        ivCurrent = ivChat;

    }



    private void initPager() {
        fragments.add(BlankFragment4.newInstance("hello"));
        fragments.add(BlankFragment4.newInstance("邹欣"));
        fragments.add(BlankFragment4.newInstance("康家慧"));
        fragments.add(BlankFragment4.newInstance("吕阳阳"));
        fragments.add(BlankFragment4.newInstance("王仪"));
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        main5Vp.setAdapter(pagerAdapter);
        main5Vp.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            //设置滚动动画
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
            //
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }


    private void changeTab(int position) {
        ivCurrent.setSelected(false);
        switch (position){
            case R.id.id_tab_weixin:
                main5Vp.setCurrentItem(0);
            case 0:
                ivCurrent = ivChat;
                ivChat.setSelected(true);
                break;
            case R.id.id_tab_contact:
                main5Vp.setCurrentItem(1);
            case 1:
                ivCurrent = ivContacts;
                ivContacts.setSelected(true);
                break;
            case R.id.id_tab_find:
                main5Vp.setCurrentItem(2);
            case 2:
                ivCurrent = ivFind;
                ivFind.setSelected(true);
                break;
            case R.id.id_tab_profile:
                main5Vp.setCurrentItem(3);
            case 3:
                ivCurrent = ivProfile;
                ivProfile.setSelected(true);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        changeTab(v.getId());
    }
}